package com.cskaoyan.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.converter.CartItemConverter;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.form.CartForm;
import com.cskaoyan.shopping.service.ICartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @auther zk
 * @date 2022/7/29 15:37
 */
@Slf4j
@RestController
@RequestMapping("/shopping")
public class CartController {

    @Autowired
    ICartService iCartService;

    /*
     * @description:查看购物车
     * @author: zhang tao
     * @date: 2022-07-30 15:09
     * @param:
     * @return:
     **/
    @GetMapping("/carts")
    //public ResponseData getCartGoods(@ResponseBody @RequestHeader("user_info") UserInfo userInfo )
    public ResponseData getCartGoods(HttpServletRequest request) {
        String userInfo = request.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        long uid = Long.parseLong(object.get("uid").toString());
        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(uid);
        CartListByIdResponse response = iCartService.getCartListById(cartListByIdRequest);
        //成功
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getCartProductDtos(), "success");
        }
        //失败
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }
    /*
     * @description:添加购物车
     * @author: zhang tao
     * @date: 2022-07-30 15:09
     * @param:
     * @return:
     **/
    @PostMapping("/carts")
    public ResponseData addCarts(@RequestBody CartForm form) {
        AddCartRequest request = new AddCartRequest();
        request.setUserId(form.getUserId());
            request.setItemId(form.getProductId());
           request.setNum(form.getProductNum());
        AddCartResponse response = iCartService.addToCart(request);
        //成功
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData("success");
        }

        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @PutMapping("/carts")
    public ResponseData updateCarts(@RequestBody CartForm form) {

        UpdateCartRequest request = CartItemConverter.cartFormToUpdateCartRequest(form);
        UpdateCartResponse response = iCartService.updateCart(request);

        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getMsg());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @DeleteMapping("/carts/**/**")
    public ResponseData deleteCarts(HttpServletRequest httpServletRequest) {
        String requestURI = httpServletRequest.getRequestURI();
        String[] split = requestURI.split("/");
        Long uid = Long.valueOf(split[split.length - 2]);
        Long pid = Long.valueOf(split[split.length - 1]);

        DeleteCartItemRequest request = new DeleteCartItemRequest();
        request.setUserId(uid);
        request.setItemId(pid);
        DeleteCartItemResponse response = iCartService.deleteCartItem(request);

        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getMsg());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @DeleteMapping("/items/**")
    public ResponseData deleteCheckedCarts(HttpServletRequest httpServletRequest) {
        String requestURI = httpServletRequest.getRequestURI();
        String[] split = requestURI.split("/");
        Long uid = Long.valueOf(split[split.length - 1]);

        DeleteCheckedItemRequest request = new DeleteCheckedItemRequest();
        request.setUserId(uid);

        DeleteCheckedItemResposne response = iCartService.deleteCheckedItem(request);

        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getMsg());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @PutMapping("/items")
    public ResponseData checkAllCartItem(@RequestBody CartForm form) {
        CheckAllItemRequest request = new CheckAllItemRequest();
        request.setUserId(form.getUserId());
        request.setChecked(form.getChecked());

        CheckAllItemResponse response = iCartService.checkAllCartItem(request);

        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getMsg());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }


}
